﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Web.Controls;
using System.Drawing;

namespace blackhouse.WebNemo.Web.Validators {

    [Serializable]
    public class ImageValidator : IValidatorSource {

        #region Fields

        private FileUpload fu;
        private string columnName;

        #endregion

        #region Properties

        public string[] AllowExtensions { get; set; }
        public Size MinimumSize { get; set; }

        #endregion

        public ImageValidator() {
            this.AllowExtensions = new string[] { "jpg", "png", "gif" };
            this.MinimumSize = Size.Empty;
        }

        public void Set(GroupRecord record, GroupColumn column, EditRecordValue control) {
            FileEditRecordValue fc = control as FileEditRecordValue;
            this.columnName = column.Name;
            if (fc != null)
                this.fu = fc.fu;
        }

        public IEnumerable<BaseValidator> GetValidators() {
            List<BaseValidator> vs = new List<BaseValidator>(1);
            if (fu != null) {
                vs.Add(this.GetImageValidator());
            }
            return vs;
        }

        private CustomValidator GetImageValidator() {
            CustomValidator cv = new CustomValidator();
            cv.ServerValidate += new ServerValidateEventHandler(cv_ServerValidate);
            cv.ErrorMessage = "Niepoprawny plik";
            return cv;
        }

        void cv_ServerValidate(object source, ServerValidateEventArgs args) {
            if (fu.HasFile) {

                bool passExt = false;
                for (int xs = 0; xs < this.AllowExtensions.Length; xs++) {
                    string ext = '.' + this.AllowExtensions[xs];
                    if (fu.PostedFile.FileName.EndsWith(ext, StringComparison.InvariantCultureIgnoreCase)) {
                        passExt = true;
                        break;
                    }
                }
                if (!passExt) {
                    args.IsValid = false;
                    BaseValidator bv = source as BaseValidator;
                    bv.ErrorMessage = "Plik (" + this.columnName + ") musi mieć rozszerzenie: " + string.Join(", ", this.AllowExtensions);
                    bv.Text = "Plik musi mieć rozszerzenie: " + string.Join(", ", this.AllowExtensions);
                    return;
                }

                bool passSize = true;
                System.Drawing.Image img = System.Drawing.Image.FromStream(fu.PostedFile.InputStream);
                if (this.MinimumSize.Width > 0 && img.Size.Width < this.MinimumSize.Width)
                    passSize = false;
                if (this.MinimumSize.Height > 0 && img.Size.Height < this.MinimumSize.Height)
                    passSize = false;

                if (!passSize) {
                    args.IsValid = false;
                    BaseValidator bv = source as BaseValidator;
                    bv.ErrorMessage = "Plik (" + this.columnName + ") musi być większy lub równy " + ((this.MinimumSize.Width > 0) ? "szerokość: " + this.MinimumSize.Width + "px, " : "") + ((this.MinimumSize.Height > 0) ? "wysokość: " + this.MinimumSize.Height + "px" : "");
                    bv.Text = "Plik  musi być większy lub równy " + ((this.MinimumSize.Width > 0) ? "szerokość: " + this.MinimumSize.Width + "px, " : "") + ((this.MinimumSize.Height > 0) ? "wysokość: " + this.MinimumSize.Height + "px" : "");
                    return;
                }
            }
        }

    }
}